﻿using System;
using System.Collections.Generic;

public class RandomUtil 
{

	public static Random random = new Random();

	public static bool GetRandomResult(int range, int num)
	{
		return num > random.Next (0, range);
	}

	public static float GetRandomFloat(float range, float addParam)
	{
		return (float)(random.NextDouble () * range + addParam);
	}

	public static int GetRandomInt(int minValue, int maxValue)
	{
		return random.Next(minValue, maxValue);
	}

	public static bool GetRandomResult(float chance)
	{
		int temp = random.Next (0,100);
		if(temp < chance * 100)
		{
			return true;
		}
		return false;
	}

	// return random index of weightList
	// if return -1, it means not find
	public static int RandomByWeight(List<int> weightList)
	{
		int NOT_FIND = -1;

		int weightCount = weightList.Count;
		if (weightCount == 0)
		{
			return NOT_FIND;
		}

		int totalWeight = 0;
		foreach (int weight in weightList)
		{
			totalWeight += weight;
		}
		if (totalWeight <= 0)
		{
			return NOT_FIND;
		}

		int randomWeight = UnityEngine.Random.Range(0, totalWeight);
		int randomIndex = -1;
		int tempWeight = 0;
		for (int i = 0; i < weightCount; i++)
		{
			tempWeight += weightList[i];
			if (tempWeight > randomWeight)
			{
				randomIndex = i;
				break;
			}
		}
		return randomIndex;
	}
}
